package com.wjb.xietong.W3;

import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
import com.wjb.xietong.Volley.toolbox.ImageLoader.ImageCache;
import com.wjb.xietong.util.LogD;

/**
 * Basic LRU Memory cache.
 *
 */
public class BitmapLruImageCache extends LruCache<String, Bitmap> implements ImageCache{
	
	public BitmapLruImageCache(int maxSize) {
		super(maxSize);
	}
	
	@Override
	protected int sizeOf(String key, Bitmap value) {
		return value.getRowBytes() * value.getHeight();
	}
	
	@Override
	public Bitmap getBitmap(String url) {
		if(null == url || "".equals(url)){
			return null;
		}
		LogD.output("Retrieved item from Mem Cache");
		return get(getCacheKey(url));
	}
 
	@Override
	public void putBitmap(String url, Bitmap bitmap) {
		if(null == url || "".equals(url) || null == bitmap){
			return;
		}
		LogD.output("Added item to Mem Cache");
		put(getCacheKey(url), bitmap);
	}
	
	/**
	 * Creates a unique cache key based on a url value
	 * 
	 * @param url
	 *            url to be used in key creation
	 * @return cache key value
	 */
	private String getCacheKey(String url) {
		return String.valueOf(url.hashCode());
	}
}
